package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {
    Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }


    public void parseMapper(InputStream resourceAsStream) throws DocumentException, ClassNotFoundException {
        Document document = new SAXReader().read(resourceAsStream);
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        //Mapper部分
        List<Element> selectElements = document.selectNodes("//select");
        for (Element selectElement : selectElements) {
            MappedStatement mappedStatement = new MappedStatement();
            String id = selectElement.attributeValue("id");
            String paramterType = selectElement.attributeValue("paramterType");
            String resultType = selectElement.attributeValue("resultType");
            if (paramterType != null && paramterType.length() > 0) {
                Class<?> paramterTypeClass = getClasstype(paramterType);
                mappedStatement.setParamter(paramterTypeClass);
            }
            Class<?> returnTypeClass = getClasstype(resultType);

            String sqlText = selectElement.getTextTrim();
            mappedStatement.setId(id);
            mappedStatement.setResultType(returnTypeClass);
            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
        }

        List<Element> updateElements = document.selectNodes("//update");
        for (Element updateElement : updateElements) {
            MappedStatement mappedStatement = new MappedStatement();
            String id = updateElement.attributeValue("id");
            String paramterType = updateElement.attributeValue("paramterType");

            if (paramterType != null && paramterType.length() > 0) {
                Class<?> paramterTypeClass = getClasstype(paramterType);
                mappedStatement.setParamter(paramterTypeClass);
            }
            String resultType = updateElement.attributeValue("resultType");
            if (resultType != null && resultType.length() > 0) {
                Class<?> returnTypeClass = getClasstype(resultType);
                mappedStatement.setResultType(returnTypeClass);
            }
            String sqlText = updateElement.getTextTrim();
            mappedStatement.setId(id);

            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
        }


        List<Element> deleteElements = document.selectNodes("//delete");
        for (Element deleteElement : deleteElements) {
            MappedStatement mappedStatement = new MappedStatement();
            String id = deleteElement.attributeValue("id");
            String paramterType = deleteElement.attributeValue("paramterType");

            if (paramterType != null && paramterType.length() > 0) {
                Class<?> paramterTypeClass = getClasstype(paramterType);
                mappedStatement.setParamter(paramterTypeClass);
            }
            String resultType = deleteElement.attributeValue("resultType");
            if (resultType != null && resultType.length() > 0) {
                Class<?> returnTypeClass = getClasstype(resultType);
                mappedStatement.setResultType(returnTypeClass);
            }
            String sqlText = deleteElement.getTextTrim();
            mappedStatement.setId(id);

            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
        }

        List<Element> insertElements = document.selectNodes("//insert");
        for (Element insertElement : insertElements) {
            MappedStatement mappedStatement = new MappedStatement();
            String id = insertElement.attributeValue("id");
            String paramterType = insertElement.attributeValue("paramterType");

            if (paramterType != null && paramterType.length() > 0) {
                Class<?> paramterTypeClass = getClasstype(paramterType);
                mappedStatement.setParamter(paramterTypeClass);
            }
            String resultType = insertElement.attributeValue("resultType");
            if (resultType != null && resultType.length() > 0) {
                Class<?> returnTypeClass = getClasstype(resultType);
                mappedStatement.setResultType(returnTypeClass);
            }
            String sqlText = insertElement.getTextTrim();
            mappedStatement.setId(id);

            mappedStatement.setSql(sqlText);
            configuration.getMappedStatementMap().put(namespace + "." + id, mappedStatement);
        }

    }

    /**
     * 根据参数类型返回，生成对应类并返回
     *
     * @param paramterType
     * @return
     * @throws ClassNotFoundException
     */
    private Class<?> getClasstype(String paramterType) throws ClassNotFoundException {
        Class<?> aClass = Class.forName(paramterType);
        return aClass;
    }
}
